package cn.me.alphamq.common.remote;

import cn.me.alphamq.common.entity.BaseMsg;
import cn.me.alphamq.common.entity.resp.*;
import cn.me.alphamq.common.enumeration.EventCodeEnum;
import cn.me.alphamq.common.msg.TcpMsg;
import io.netty.channel.ChannelHandler;
import io.netty.channel.ChannelHandlerContext;
import io.netty.channel.SimpleChannelInboundHandler;

/**
 * nameserver 发送的响应的处理器，可以被多个线程共享
 *
 * @author f
 */
@ChannelHandler.Sharable
public class NameServerRespHandler extends SimpleChannelInboundHandler<TcpMsg> {

    @Override
    protected void channelRead0(ChannelHandlerContext ctx, TcpMsg tcpMsg) throws Exception {
        int code = tcpMsg.getCode();

        Class<? extends BaseMsg> clazz;
        if (EventCodeEnum.ERROR_ACCOUNT_RESP.getCode() == code) {
            clazz = ErrorAccountResp.class;
        } else if (EventCodeEnum.REGISTRY_SUCCESS_RESP.getCode() == code) {
            clazz = ServiceRegistryResp.class;
        } else if (EventCodeEnum.HEART_BEAT_SUCCESS_RESP.getCode() == code) {
            clazz = ServiceHeartBeatResp.class;
        } else if (EventCodeEnum.PULL_BROKER_ADDRESS_RESP.getCode() == code) {
            clazz = PullBrokerAddressResp.class;
        } else if (EventCodeEnum.LIST_ALL_TOPIC_INFO_RESP.getCode() == code) {
            clazz = ListAllTopicInfoResp.class;
        } else {
            throw new RuntimeException("invalid msg code");
        }

        SyncFuture.removeAndSetResp(clazz, tcpMsg);
    }
}
